﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GDI_学习_练习
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void DrawSin()
        {
            int x1, x2;
            double y1, y2;
            double a;
            Pen myPen = new Pen(Color.Blue, 3);
            x1 = x2 = 0;
            y1 = y2 = this.panel1.Width / 2;
            for (x2 = 0; x2 < this.panel1.Width; x2++)
            {
                a = 2 * Math.PI * x2 / this.panel1.Width;
                y2 = Math.Sin(a);
                y2 = (1 - y2) * this.panel1.Height / 2;
                this.panel1.CreateGraphics().DrawLine(myPen, x1, (float)y2, x2, (float)y2);
                x1 = x2;
                y1 = y2;
            }
        }

        delegate double DrawMath(double a);
        private void Draw(Color color, DrawMath drawMath)
        {
            int x1, x2;
            double y1, y2;
            double a;
            Pen myPen = new Pen(color, 3);
            x1 = x2 = 0;
            y1 = y2 = this.panel1.Width / 2;
            for (x2 = 0; x2 < this.panel1.Width; x2++)
            {
                a = 2 * Math.PI * x2 / this.panel1.Width;
                y2 = drawMath(a);
                y2 = (1 - y2) * this.panel1.Height / 2;
                this.panel1.CreateGraphics().DrawLine(myPen, x1, (float)y2, x2, (float)y2);
                x1 = x2;
                y1 = y2;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.comboBox1.SelectedIndex = 1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            switch (comboBox1.Text)
            {
                case "y=sin(x)":
                    Draw(Color.Black, Math.Sin);
                    break;
                case "y=cos(x)":
                    Draw(Color.Red, Math.Cos);
                    break;
                case "y=tan(x)":
                    Draw(Color.CadetBlue, Math.Tan);
                    break;
                default:
                    break;
            }
        }
    }
}
